package a02;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class VierBoard<M> extends RegularBoard<M> implements Constants {

    private static final long serialVersionUID = 1L;
    private Graphics2D graphics;
    private Color colorNone = Color.WHITE;

    public VierBoard(RegularGame<M> game) {
	super(game);
    }

    @Override
    public void paintComponent(Graphics g1) {
	super.paintComponent(g1);
	this.graphics = (Graphics2D) g1;
	if(Config.ANTIALIASING) this.graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		RenderingHints.VALUE_ANTIALIAS_ON);

	for (byte c = 0; c < this.getGame().getColumns(); c++) {
	    for (byte r = 0; r < this.getGame().getRows(); r++) {
		if (isShowBlanksActive()) {
		    if (this.getGame().getAtPosition(c, r) != this.getGame()
			    .getPlayerNone()) {
			this.graphics.setColor(this.selectColor(this.getGame()
				.getAtPosition(c, r)));
			this.graphics.fillOval((c * this.getUNIT() + 5), (r
				* this.getUNIT() + 5), (this.getUNIT() - 10),
				(this.getUNIT()) - 10);
		    } else if (this.getGame().getAtPosition(c, r) == this
				    .getGame().getPlayerNone()) {
			this.graphics.setColor(colorNone);
			this.graphics.fillOval((c * this.getUNIT() + 5), (r
				* this.getUNIT() + 5), (this.getUNIT() - 10),
				(this.getUNIT()) - 10);
		    }
		} else {
		    if (this.getGame().getAtPosition(c, r) != this.getGame()
			    .getPlayerNone()) {
			paintStone(c, r);
		    }
		}
		paintLastMove(c, r);
	    }
	}
	if (getControl().isCurrentPlayerWinner()) {
	    paintWinningLine();
	}
    }

    private void paintWinningLine() {
	int x1 = getControl().getWinningLine().get(0).fst;
	int y1 = getControl().getWinningLine().get(0).snd;
	int x2 = getControl().getWinningLine().get(getControl().getWinningLine().size()- 1).fst;
	int y2 = getControl().getWinningLine().get(getControl().getWinningLine().size()- 1).snd;
	this.graphics.setColor(new Color(11, 116, 0));
	this.graphics.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND,
		BasicStroke.JOIN_BEVEL));
	this.graphics.drawLine((x1 * this.getUNIT()) + (this.getUNIT() / 2),
		(y1 * this.getUNIT()) + (this.getUNIT() / 2),
		(x2 * this.getUNIT()) + (this.getUNIT() / 2),
		(y2 * this.getUNIT()) + (this.getUNIT() / 2));
    }
}
